package com.lecture.java8.excel.dyr.j;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.DemoDataListener;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Test0729 {

    public static void main(String[] args) {
        String dir = "D:\\study\\tmp\\0729\\";


        String resultPath = dir + "2013年带行业变量的.xlsx";
        DemoDataListener<Model0729> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(resultPath, Model0729.class,dataResultListener).sheet().doRead();
        //全要素生产率
        List<Model0729> resultList = dataResultListener.getDataList();

        resultList = filterCompleteYears(resultList,2013,2022);



        EasyExcel.write( dir + "0729-1结果.xlsx", Model0729.class)
                .sheet("a").doWrite(resultList);

    }

    public static List<Model0729> filterCompleteYears(List<Model0729> list, int startYear, int endYear) {
        Set<Integer> requiredYears = IntStream.rangeClosed(startYear, endYear)
                .boxed()
                .collect(Collectors.toSet());

        // Group by code and filter
        Map<String, Set<Integer>> codeToYearsMap = list.stream()
                .collect(Collectors.groupingBy(
                        Model0729::getA,
                        Collectors.mapping(Model0729::getC, Collectors.toSet())
                ));


        Set<String> validCodes = codeToYearsMap.entrySet().stream()
                .filter(entry -> entry.getValue().equals(requiredYears))
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());

        // Filter original list by valid codes
        return list.stream()
                .filter(obj -> validCodes.contains(obj.getA()))
                .collect(Collectors.toList());
    }
}
